/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package src.view.deposit;

import java.io.Serializable;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;
import org.primefaces.context.RequestContext;
import org.primefaces.event.CloseEvent;
import src.domain.deposit.Bilyet;
import src.domain.saving.Saving;
import src.domain.security.Users;
import src.services.deposit.BilyetModel;
import src.services.deposit.DeproductModel;
import src.services.saving.SavingModel;
import src.services.security.UsersModel;
import src.util.Constanta;
import src.util.Messager;

/**
 *
 * @author Yusup Jauhari Shandi <in.visi>
 */

@ManagedBean
@SessionScoped
public class DepositInfoBean implements Serializable{
    private Bilyet bilyet;
    private List<Bilyet> bilyets;
    private List<Bilyet> bilyetSimulation;
     
    private BilyetModel bilHelper;
    private List<SelectItem> avblProd, avblDisb;
    private String accntDest, bilyetParentNo, accntOwner, depSavaccnt, depSavaccntOwner;
    private String filter;
    private String taxAmounts, interestAmounts;   
        
    public DepositInfoBean() {
      this.bilHelper = new BilyetModel();
      this.avblDisb = Constanta.getDepDisb();
    }

    public String getFilter() {
        return filter;
    }

    public void setFilter(String filter) {
        this.filter = filter;
    }
    
    
    public Bilyet getBilyet() {
        return bilyet;
    }

    public void setBilyet(Bilyet bilyet) {
        this.bilyet = bilyet;
    }
    
    public String getAccntDest() {
        return accntDest;
    }

    public String getBilyetParentNo() {
        return bilyetParentNo;
    }

    public String getAccntOwner() {
        return accntOwner;
    }

    public String getDepSavaccnt() {
        return depSavaccnt;
    }

    public void setDepSavaccnt(String depSavaccnt) {
        this.depSavaccnt = depSavaccnt;
    }

    public String getDepSavaccntOwner() {
        return depSavaccntOwner;
    }

    public void setDepSavaccntOwner(String depSavaccntOwner) {
        this.depSavaccntOwner = depSavaccntOwner;
    }

    
    public List<SelectItem> getAvblDisb() {
        return avblDisb;
    }

    public List<SelectItem> getAvblProd() {
        return avblProd;
    }

    public List<Bilyet> getBilyets() {
        return bilyets;
    }
        
    public void initForm(){
        this.avblProd = new DeproductModel().listAll();
        
    }
    
    public void handleClose(CloseEvent event) {
        this.bilyet = null;
        this.bilyetSimulation = null;
        
        
    }
    
    public void loadSavings () {
        
        UsersModel userHelper = new UsersModel();
        Integer userId = Integer.parseInt(FacesContext.getCurrentInstance()
                .getExternalContext().getRequestParameterMap().get("userId"));
        
        Users user = userHelper.getById(userId);
        
        Map<String, Object> savfilter = new HashMap<>();
        Map<Object, Object> amtfilter = new HashMap<>();
        amtfilter.put("<=", user.getUgroup().getDepMaxDeposit());
      //  savfilter.put("authorizedUid", null);
        savfilter.put("closeDate", null);
        savfilter.put("principleAmount", amtfilter);
        this.bilyets = bilHelper.findAll(savfilter, " order by bilyetDate");
    }
    
    
    public void search() {
        StringBuilder sqlFull = new StringBuilder();
        sqlFull.append("from Bilyet bilyet ");                
        sqlFull.append("where closeDate is null ");
        //sqlFull.append("and closedBy is null ");
        
        if (!filter.equals(""))
             sqlFull.append(" and (upper(customer.customerName) like '%").append(filter).append("%' or ").append(" bilyetNo like '%").append(filter).append("%') ");
        sqlFull.append("order by bilyetNo");
        this.bilyets = bilHelper.search(sqlFull.toString());
    }
    
    public void openData(){
        initForm();
        if (bilyet.getSavaccntId() != null) {
            Saving sav = new SavingModel().getById(bilyet.getSavaccntId());
            accntDest = sav.getAccountNo();
            accntOwner = sav.getCustomer().getCustomerName();
        }
        
        if (bilyet.getParentBilyetId() != null) {
            Bilyet bil = bilHelper.getById(bilyet.getSavaccntId());
            bilyetParentNo = bil.getBilyetNo();
        }
        BilyetsimBean sim = new BilyetsimBean();
        sim.setBilyet(bilyet);
        sim.simulasi();
        this.bilyetSimulation = sim.getBilyets();
        this.taxAmounts = sim.getTaxAmounts();
        this.interestAmounts = sim.getInterestAmounts();
        
    }

    public List<Bilyet> getBilyetSimulation() {
        return bilyetSimulation;
    }

    public void setBilyetSimulation(List<Bilyet> bilyetSimulation) {
        this.bilyetSimulation = bilyetSimulation;
    }

    public String getInterestAmounts() {
        return interestAmounts;
    }

    public void setInterestAmounts(String interestAmounts) {
        this.interestAmounts = interestAmounts;
    }

    public String getTaxAmounts() {
        return taxAmounts;
    }

    public void setTaxAmounts(String taxAmounts) {
        this.taxAmounts = taxAmounts;
    }
    
    
    
    public void approve(boolean approved, Integer userId) {
        RequestContext context = RequestContext.getCurrentInstance();
        boolean success;  
        
        bilyet.setAuthorizedUid(userId);
        if (approved==false) bilyet.setCloseDate(new Date());
        
        success = bilHelper.saveData(bilyet);
        
        if (success) {
            this.bilyets.remove(this.bilyet);
            this.bilyet = null;
        } 
        
        Messager.showSaveUpdateStatus(success);     
        context.addCallbackParam("success", success);  
    }
    
}
